package com.jeecg.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeecg.bean.BaseBean;
import com.jeecg.bean.vo.MVCVO;
import com.jeecg.control.BaseAction;
import com.jeecg.util.FullBeanUtil;

public class strutsPreAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public strutsPreAction() {
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
/**
 * 拦截所有请求的action,并做以下操作
 *1、set上对象属性
 *2、接受具体action返回的跳转页面，然后跳转
 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath=request.getServletPath();
		String sp=servletPath.split("\\.")[0];
		Map <String,MVCVO> map=(Map <String,MVCVO>)request.getServletContext().getAttribute("struts");
		MVCVO strutsVO= map.get(sp);
		BaseBean baseForm=(BaseBean)FullBeanUtil.full(request,strutsVO);
		Class clazz;
		BaseAction action=null;
		try {
			clazz = Class.forName(strutsVO.getActionClass());
			action=(BaseAction)clazz.newInstance();
			String result=action.execute(request,response,baseForm);
			Map<String,String> redirctMap=strutsVO.getDispUrl();
			String redirctUrl=redirctMap.get(result);
			RequestDispatcher dispat=request.getRequestDispatcher(redirctUrl);
			dispat.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}

}
